package com.taiyi.renaissancemall.ui.mine.order.ask_every_one.answer;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.AskEveryoneListBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//点击问大家列表进入答案列表 可回答(4-40)
public class AnswerListActivity extends BaseActivity<AnswerView, AnswerPresent> implements AnswerView {


    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.tv_answer)
    TextView tvAnswer;
    @BindView(R.id.ll_answer)
    LinearLayout llAnswer;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    @BindView(R.id.edt_answer)
    EditText edtAnswer;
    @BindView(R.id.btn_commit_answer)
    Button btnCommitAnswer;
    @BindView(R.id.ll_commit)
    LinearLayout llCommit;
    @BindView(R.id.rcl_answer)
    RecyclerView rclAnswer;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.tv_answer_num)
    TextView tvAnswerNum;
    @BindView(R.id.tv_answer_date)
    TextView tvAnswerDate;
    @BindView(R.id.ll_answer_xia)
    LinearLayout llAnswerXia;
    private AnswerListAdapter answerListAdapter;
    private String commodityid;
    private int position;
    private String comproblemid;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_answer_list;
    }

    @Override
    public AnswerPresent initPresenter() {
        return new AnswerPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        commodityid = getIntent().getStringExtra("commodityid");
        position = getIntent().getIntExtra("position", -1);

        titleTv.setText(getString(R.string.answerList));

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        showProgress();
        presenter.getAskList("1", commodityid);
    }

    @Override
    public void initEvent() {

    }

    @OnClick({R.id.backIv, R.id.btn_commit_answer})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.btn_commit_answer:
                if (TextUtils.isEmpty(edtAnswer.getText().toString().trim())) {
                    ToastUtils.showShortToast("请输入回答");
                    return;
                }
                showProgress();
                presenter.addAnswer(commodityid, comproblemid, edtAnswer.getText().toString().trim());
                break;
        }
    }

    @Override
    public void getAskListSuccess(AskEveryoneListBean askEveryoneListBean) {
        hideProgress();
        if (askEveryoneListBean.welist != null
                && askEveryoneListBean.welist.get(position).anslist != null
                && askEveryoneListBean.welist.get(position).anslist.size() > 0) {
            noDataIv.setVisibility(View.GONE);
            setAdapter(askEveryoneListBean.welist.get(position).anslist);
        } else {
            noDataIv.setVisibility(View.VISIBLE);
        }
        comproblemid = askEveryoneListBean.welist.get(position).comproblemid;
        tvAnswer.setText(askEveryoneListBean.welist.get(position).problemcontent);
        String num = "";
        if (askEveryoneListBean.welist.get(position).anslist != null
                && askEveryoneListBean.welist.get(position).anslist.size() > 0) {
            num = "全部" + askEveryoneListBean.welist.get(position).anslist.size() + "个回答";
        } else {
            num = "全部0个问题";
        }
        tvAnswerNum.setText(num);
        tvAnswerDate.setText(askEveryoneListBean.welist.get(position).createtime);
    }

    @Override
    public void getAskListFailure(String msg) {
        hideProgress();
        ToastUtils.showShortToast(msg);
    }

    @Override
    public void addAnswerSuccess(String msg_cn) {
        ToastUtils.showShortToast(msg_cn);
        edtAnswer.setText("");
        presenter.getAskList("1", commodityid);
    }

    @Override
    public void addAnswerFailure(String msg) {
        hideProgress();
        ToastUtils.showShortToast(msg);
    }

    private void setAdapter(List<AskEveryoneListBean.WelistBean.AnslistBean> anslistBeans) {
        answerListAdapter = new AnswerListAdapter(R.layout.layout_answer_list, anslistBeans);
        rclAnswer.setLayoutManager(new LinearLayoutManager(AnswerListActivity.this, LinearLayoutManager.VERTICAL, false));
        rclAnswer.setAdapter(answerListAdapter);
    }

}
